﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MallGo.Models;

namespace MallGo.Controllers
{
    public class UserController : Controller
    {
        private UserContext db = new UserContext();

        ////
        //// GET: /User/

        public ActionResult index()
        {
            //var model = db.User.ToList();
            return View();
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterModel RegisterModel)
        {
            IEnumerable<User> u = db.User.Where(user => user.UName == RegisterModel.UserName);
            if (u.Count() > 0)
            {
                return RedirectToAction("Index");
            }
            else
            {
                User user = new User();
                user.UName = RegisterModel.UserName;
                user.UPassword = RegisterModel.Password;
                db.User.Add(user);
                db.SaveChanges();
                return RedirectToAction("List");
            }
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginModel LoginModel)
        {
            IEnumerable<User> u = db.User.Where(user => user.UName == LoginModel.UserName && user.UPassword == LoginModel.Password);
            if (u.Count() == 0)
            {
                Response.Write("Wrong password!");
                return View();
            }
            else return RedirectToAction("List");
        }

        public ActionResult List()
        {
            List<User> users = db.User.ToList();
            return View(users);
        }
    }
}